#include "DecoFluidBody.h"
#include "RenderInterface.h"
#include "LzmaDecode.h"
#include "ConfigManager.h"
#ifdef _SLC
#include "slcSurface.H"
#endif

DecoFluidObject::DecoFluidObject(void)
:DecoSceneObject(FluidT)
{
	bIsRenderable = TRUE;
	bIsMovable = TRUE;
	bIsSelectable = TRUE;
	bIsRotatable = TRUE;
}

DecoFluidObject::~DecoFluidObject(void)
{
}


DecoFluidObject* DecoFluidObject::CreateFromMesh(const std::string & path)
{
	ifstream testStrm(path.c_str());
	if(testStrm.fail())
		return NULL;
	testStrm.close();
	char lowerpath[256];
	size_t i;
	for(i = 0; i < path.length(); ++i)
	{
		lowerpath[i] = tolower(path[i]);
	}
	lowerpath[i] = '\0';
	if(strstr(lowerpath, ".bdt") > 0)
	{
		matrix44 mat = IdentityMatrix44();
#ifdef _SLC
		SLC::GetSingleton()->UnionMesh(lowerpath, mat);
		//SLC::GetSingleton()->ReadBdt(lowerpath);
#endif
		return NULL;
	}
	if(strstr(lowerpath, ".obj") > 0)
	{
		matrix44 mat = IdentityMatrix44();
#ifdef _SLC
		//SLC::GetSingleton()->ReadObj(lowerpath);
		SLC::GetSingleton()->UnionMesh(lowerpath, mat);
#endif
		return NULL;
	}
	DecoFluidObject* retval = new DecoFluidObject();
	if(!retval->CreateSceneObjectFromMesh(path))
	{
		delete retval;
		return NULL;
	}
	return retval;
}

#ifdef MFC_ENABLED
//XML related operations
BOOL DecoFluidObject::LoadFromXML(CXmlNodeWrapper& node)
{
	return DecoSceneObject::LoadFromXML(node);
}
BOOL DecoFluidObject::SaveToXML(CXmlNodeWrapper& node)const
{
	return DecoSceneObject::SaveToXML(node);
}
#endif

DecoSprayerObject::DecoSprayerObject(void)
:DecoSceneObject(SprayerT)
{
	bIsRenderable = TRUE;
	bIsMovable = TRUE;
	bIsSelectable = TRUE;
	bIsRotatable = FALSE;
}

DecoSprayerObject::~DecoSprayerObject(void)
{
}


DecoSprayerObject* DecoSprayerObject::CreateFromMesh(const std::string & path)
{
	ifstream testStrm(path.c_str());
	if(testStrm.fail())
		return NULL;
	testStrm.close();
	DecoSprayerObject* retval = new DecoSprayerObject();
	if(!retval->CreateSceneObjectFromMesh(path))
	{
		delete retval;
		return NULL;
	}
	return retval;
}

DecoFreerObject::DecoFreerObject(void)
:DecoSceneObject(FreerT)
{
	bIsRenderable = TRUE;
	bIsMovable = TRUE;
	bIsSelectable = TRUE;
	bIsRotatable = FALSE;
}

DecoFreerObject::~DecoFreerObject(void)
{

}


DecoFreerObject* DecoFreerObject::CreateFromMesh(const std::string & path)
{
	ifstream testStrm(path.c_str());
	if(testStrm.fail())
		return NULL;
	testStrm.close();
	DecoFreerObject* retval = new DecoFreerObject();
	if(!retval->CreateSceneObjectFromMesh(path))
	{
		delete retval;
		return NULL;
	}
	return retval;
}

DecoArchive& operator<< (DecoArchive& Ar, const DecoFluidObject& obj)
{
	const DecoSceneObject* const scnObj = &obj;
	scnObj->Serialize(Ar);
	return Ar;

}
DecoArchive& operator>> (DecoArchive& Ar, DecoFluidObject& obj)
{
	DecoSceneObject* scnObj = &obj;
	scnObj->Deserialize(Ar);
	return Ar;
}
DecoArchive& operator<< (DecoArchive& Ar, const DecoSprayerObject& obj)
{
	const DecoSceneObject*  const scnObj = &obj;
	scnObj->Serialize(Ar);
	return Ar;
}

DecoArchive& operator>> (DecoArchive& Ar, DecoSprayerObject& obj)
{
	DecoSceneObject* scnObj = &obj;
	scnObj->Deserialize(Ar);
	return Ar;
}

DecoArchive& operator<< (DecoArchive& Ar, const DecoFreerObject& obj)
{
	const DecoSceneObject*  const scnObj = &obj;
	scnObj->Serialize(Ar);
	return Ar;
}

DecoArchive& operator>> (DecoArchive& Ar, DecoFreerObject& obj)
{
	DecoSceneObject* scnObj = &obj;
	scnObj->Deserialize(Ar);
	return Ar;
}